/**
 *return the first argument if set, the second if not, "" if none of both is set
 */
function iif()
{
    return arguments[0] ? arguments[0] : (arguments[1] ? arguments[1] : "");
}

//---------------------------------------------------------------------
//----- Functions
//---------------------------------------------------------------------

if (navigator.product == "Gecko") {
	Document.prototype.elementFromPoint = function(x, y) {
		this.addEventListener("mousemove", this.elementFromPoint__handler, false);
		var event = this.createEvent("MouseEvents");
		var box = this.getBoxObjectFor(this.documentElement);
		var screenDelta = { x: box.screenX, y: box.screenY };
		event.initMouseEvent("mousemove", true, false, this.defaultView, 0,
		                      x + screenDelta.x, y + screenDelta.y, x, y,
		                      false, false, false, false, 0, null);
		this.dispatchEvent(event);
		this.removeEventListener("mousemove", this.elementFromPoint__handler, false);
		return this.elementFromPoint__target;
	}
	Document.prototype.elementFromPoint__handler = function (event) {
		this.elementFromPoint__target = event.explicitOriginalTarget;

		// reparent target if it is a text node to emulate IE's behavior
		if (this.elementFromPoint__target.nodeType == Node.TEXT_NODE)
			this.elementFromPoint__target = this.elementFromPoint__target.parentNode;

		// change an HTML target to a BODY target to emulate IE's behavior (if we are in an HTML document)
		if (this.elementFromPoint__target.nodeName.toUpperCase() == "HTML" && this.documentElement.nodeName.toUpperCase() == "HTML")
			this.elementFromPoint__target = this.getElementsByTagName("BODY").item(0);

		event.preventDefault();
		event.stopPropagation();
	}
	Document.prototype.elementFromPoint__target = null;
}


function bsGlobalMouseHandler(e)
{
    if(typeof bsGlobalMouseLoc == "undefined") {
        top.bsGlobalMouseLoc = {x: 0, y: 0};
    }
    var xPos    =  (e && e.pageX)? e.pageX : window.event.x;
	var yPos    =  (e && e.pageY) ? e.pageY : window.event.y;

    bsGlobalMouseLoc.x = parseInt(xPos);
    bsGlobalMouseLoc.y = parseInt(yPos);

    if (top.bsDraggedObject) {
        bsPlace(top.bsDraggedObject.getView(), (bsGlobalMouseLoc.x-top.bsOffsetX), (bsGlobalMouseLoc.y-top.bsOffsetY));
    }
    if (top.domSelect) {
        elem = document.elementFromPoint(bsGlobalMouseLoc.x, bsGlobalMouseLoc.y);
        if (top.lastElem == null && elem != null) {
            top.lastElem = {element: elem, border: elem.style.border, padding: elem.style.padding, paddingTop: elem.style.paddingTop};
        }
        if (top.lastElem != elem) {
            top.lastElem.element.style.border = top.lastElem.border;
            top.lastElem.element.style.padding = top.lastElem.padding;
            top.lastElem.element.style.paddingTop = top.lastElem.paddingTop;
            top.lastElem = {element: elem, border: elem.style.border, padding: elem.style.padding, paddingTop: elem.style.paddingTop};
            elem.style.border = "2px solid red";
            elem.style.padding = "-2px";
        }
    }
        
    if (document.layers) routeEvent(e);
}


function bsGlobalDrop()
{
    if(top.bsDraggedObject) {
        top.bsDraggedObject.onDrop();
        top.bsDraggedObject = null;
    }
    document.onmouseup = null;
}


//Plaziert elem an der stelle left, top (abhängig vom Container, in dem elem ist)
function bsPlace(elem, left, top) {
    elem.style.left = left + "px";
    elem.style.top =  top  + "px";
}

function bsAddEvent(element, event, callback)
{
    if (element.addEventListener) {
        element.addEventListener(event, callback, false);
    } else {
        element.attachEvent(event, callback);
    }
}

function bsRemoveEvent(element, event, callback)
{
    if (element.removeEventListener) {
        element.removeEventListener(event, callback, false);
    } else {
        element.detachEvent("on" + event, callback);
    }
}


bsAddEvent(document.body, "mousemove", bsGlobalMouseHandler);

